package inputstream;


import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

//演示FileInputStream字节输入流
public class FileInputStream_ {


    public static void main(String[] args) {

    }
//单个字节读取效率底
    @Test
    public void readFile01(){
        String filePath="D:\\xuexi\\Hello.txt";
       int readDate=0;
        FileInputStream fileInputStream=null;
        try {
            //创建FileInputStream对象用于读取文件
             fileInputStream = new FileInputStream(filePath);
           //从该输入流读取一个字节的数据，如果没有输入可用，此方法将阻止
            //如果返回-1表示读取完毕
            while ((readDate=fileInputStream.read())!=-1){
               // System.out.print((char)readDate);
                System.out.print(readDate);
            };
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭文件流
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    //用数组读取文件
    @Test
    public void readFile02(){
        String filePath="D:\\xuexi\\Hello.txt";
        int readLen=0;
       byte[] buf=new byte[8];
        FileInputStream fileInputStream=null;

        try {
            //创建FileInputStream对象用于读取文件

            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取一个字节的数据，如果没有输入可用，此方法将阻止
            //如果返回-1表示读取完毕
            while ((readLen=fileInputStream.read(buf))!=-1){
                System.out.print(new String(buf,0,readLen));
            };
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭文件流
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


}
